package com.allways.dubbo.mdm.service.company.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.allways.dubbo.mdm.beans.DataChangeLog;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import com.allways.dubbo.mdm.service.log.IDataChangeLogService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.company.Company;
import com.allways.dubbo.mdm.service.company.ICompanyService;

/**
 * <p>ClassName: ICompanyService</p>
 * <p>Description: 公司Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-10-19</p>
 */
@Service("mdmCompanyService")
public class CompanyServiceImpl extends MdmBaseDaoImpl implements ICompanyService {
	Log log = LogFactory.getLog(CompanyServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
     @Autowired
    IDataChangeLogService dataChangeLogService;
    
    @Override
    public int addCompany(Company company) {
   		log.debug("For the new company parameters:"+ company.toJson());
   		validColumnLen(company);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(company.getCompanyCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getCompanyByCode(company.getCompanyCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        int cnt=insert("company.insertCompany",company);
        
        /*数据变更日志*/
        DataChangeLog log = new DataChangeLog();
        log.setDataType(DataChangeLog.DataType.company.getName());
        log.setDataPk(company.getCompanyId().toString());
        log.setModifyType(DataChangeLog.OPER_TYPE_ADD);
        dataChangeLogService.addDataChangeLog(log);
        
        return cnt;
    }

    @Override
    public int updateCompany(Company company) {
    	log.debug("Update for the company parameters:"+ company.toJson());
    	validColumnLen(company);
     	//无code的不能更新
        if(StringUtils.isEmpty(company.getCompanyCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        Company oldCompany = getCompanyByCode(company.getCompanyCode());
        if(null == oldCompany){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldCompany.setId(company.getId());
		oldCompany.setCompanyCode(company.getCompanyCode());
		oldCompany.setCompanyName(company.getCompanyName());
		oldCompany.setCompanyType(company.getCompanyType());
		oldCompany.setCompanyShortName(company.getCompanyShortName());
		oldCompany.setCompanyProp(company.getCompanyProp());
		oldCompany.setParentId(company.getParentId());
		oldCompany.setCompanyIndustry(company.getCompanyIndustry());
		oldCompany.setLicenseNo(company.getLicenseNo());
		oldCompany.setLicenseNoImg(company.getLicenseNoImg());
		oldCompany.setTaxNo(company.getTaxNo());
		oldCompany.setAjCompanyClass(company.getAjCompanyClass());
		oldCompany.setCorporation(company.getCorporation());
		oldCompany.setCeo(company.getCeo());
		oldCompany.setCeoIdcard(company.getCeoIdcard());
		oldCompany.setCeoMobile(company.getCeoMobile());
		oldCompany.setCeoIdcardAttachA(company.getCeoIdcardAttachA());
		oldCompany.setCeoIdcardAttachB(company.getCeoIdcardAttachB());
		oldCompany.setCertificateStatus(company.getCertificateStatus());
		oldCompany.setCertificateTime(company.getCertificateTime());
		oldCompany.setCertificatedTime(company.getCertificatedTime());
		oldCompany.setStatus(company.getStatus());
		oldCompany.setCountryCode(company.getCountryCode());
		oldCompany.setProvinceCode(company.getProvinceCode());
		oldCompany.setCityCode(company.getCityCode());
		oldCompany.setCountyCode(company.getCountyCode());
		oldCompany.setPostcode(company.getPostcode());
		oldCompany.setStreet(company.getStreet());
		oldCompany.setAddress(company.getAddress());
		oldCompany.setBankName(company.getBankName());
		oldCompany.setBankAccount(company.getBankAccount());
		oldCompany.setDataSource(company.getDataSource());
		oldCompany.setRemark(company.getRemark());
		oldCompany.setIsDisabled(company.getIsDisabled());
		oldCompany.setCreatedBy(company.getCreatedBy());
		oldCompany.setCreatedTime(company.getCreatedTime());
		oldCompany.setUpdatedBy(company.getUpdatedBy());
		oldCompany.setUpdatedTime(company.getUpdatedTime());
        
        int cnt= update("company.updateCompany",oldCompany);
        /*数据变更日志*/
        DataChangeLog log = new DataChangeLog();
        log.setDataType(DataChangeLog.DataType.Company.getName());
        log.setDataPk(oldCompany.getCompanyId().toString());
        log.setModifyType(DataChangeLog.OPER_TYPE_EDIT);
        dataChangeLogService.addDataChangeLog(log);
        
        return cnt;
    }

    @Override
    public int deleteCompany(Company company) {
    	log.debug("Delete for the company parameters:"+ company.toJson());
    	 
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(company.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
     	//无code的不能删除
        if(StringUtils.isEmpty(company.getCompanyCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        Company fullCompany = getCompanyByCode(company.getCompanyCode());
        
        /* 数据变更日志 */
        DataChangeLog log = new DataChangeLog();
        log.setDataType(DataChangeLog.DataType.Company.getName());
        log.setDataPk(fullCompany.getCompanyId().toString());
        log.setModifyType(DataChangeLog.OPER_TYPE_DELETE);
        dataChangeLogService.addDataChangeLog(log);
        
        return delete("company.deleteCompany",company);
    }

    @Override
    public Company getCompanyByCode(String code) {
    	log.debug("Query for company code:" + code);
        return queryForObject("company.getCompanyByCode",code);
    }
    @Override
    public long getCompanyCount(Company company) {
    	log.debug("Query for company count parameters:" + company.toJson());
        return queryForObject("company.getCompanyCount",company);
    }
   @Override
    public List<Company> queryCompanys(Company company, int firstRow, int lastRow) {
      	log.debug("Query for company parameters:" + company.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("company",company);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("company.queryCompanys", param);
    }
    
    /**
     * 验证字段长度
     * @param company 需要验证的对象
     */
    private void validColumnLen(Company company){
		if(!StringUtils.isEmpty(company.getId()) && company.getId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(company.getCompanyCode()) && company.getCompanyCode().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(company.getCompanyName()) && company.getCompanyName().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(company.getCompanyType()) && company.getCompanyType().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(company.getCompanyShortName()) && company.getCompanyShortName().length()>10){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{10},null);
        }
		if(!StringUtils.isEmpty(company.getCompanyProp()) && company.getCompanyProp().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(company.getParentId()) && company.getParentId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(company.getCompanyIndustry()) && company.getCompanyIndustry().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(company.getLicenseNo()) && company.getLicenseNo().length()>20){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{20},null);
        }
		if(!StringUtils.isEmpty(company.getLicenseNoImg()) && company.getLicenseNoImg().length()>100){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{100},null);
        }
		if(!StringUtils.isEmpty(company.getTaxNo()) && company.getTaxNo().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(company.getAjCompanyClass()) && company.getAjCompanyClass().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(company.getCorporation()) && company.getCorporation().length()>10){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{10},null);
        }
		if(!StringUtils.isEmpty(company.getCeo()) && company.getCeo().length()>10){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{10},null);
        }
		if(!StringUtils.isEmpty(company.getCeoIdcard()) && company.getCeoIdcard().length()>20){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{20},null);
        }
		if(!StringUtils.isEmpty(company.getCeoMobile()) && company.getCeoMobile().length()>15){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{15},null);
        }
		if(!StringUtils.isEmpty(company.getCeoIdcardAttachA()) && company.getCeoIdcardAttachA().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(company.getCeoIdcardAttachB()) && company.getCeoIdcardAttachB().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(company.getCertificateStatus()) && company.getCertificateStatus().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(company.getCertificateTime()) && company.getCertificateTime().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(company.getCertificatedTime()) && company.getCertificatedTime().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(company.getStatus()) && company.getStatus().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(company.getCountryCode()) && company.getCountryCode().length()>5){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{5},null);
        }
		if(!StringUtils.isEmpty(company.getProvinceCode()) && company.getProvinceCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(company.getCityCode()) && company.getCityCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(company.getCountyCode()) && company.getCountyCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(company.getPostcode()) && company.getPostcode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(company.getStreet()) && company.getStreet().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(company.getAddress()) && company.getAddress().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
		if(!StringUtils.isEmpty(company.getBankName()) && company.getBankName().length()>20){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{20},null);
        }
		if(!StringUtils.isEmpty(company.getBankAccount()) && company.getBankAccount().length()>20){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{20},null);
        }
		if(!StringUtils.isEmpty(company.getRemark()) && company.getRemark().length()>50){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{50},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(company.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
